﻿using Anno.DynamicApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Anno.DynamicApi
{
    public static class Extensions
    {
        //#region AddDynamicController
        //public static ServerHandler S_ServerHandler = null;
        ///// <summary>
        ///// 判断API是否加载过，否则重新加载API
        ///// </summary>
        ///// <param name="server"></param>
        //public static bool ChekckDynamicController(ServerHandler server)
        //{
        //    if (ControllerList.DicApiInfo.Count<=0)
        //    {
        //        server.AddDynamicController(3);
        //        return true;
        //    }
        //    return false;
        //}
        ///// <summary>
        ///// 加载动态API
        ///// </summary>
        ///// <param name="server"></param>
        ///// <returns></returns>
        //public static ServerHandler AddDynamicController(this ServerHandler server, int sourceType = -1)
        //{
        //    try
        //    {
        //        var msg = ""; 
        //        var cl= new ControllerList();
        //        if (!cl.LoadApiControllers(out msg))
        //            throw new Exception("LoadApiControllers Error:" + msg);
        //        foreach (var controllerBuilderInfo in ControllerList.ControllerBuilderInfos)
        //        {
        //            LoadApiController(server, controllerBuilderInfo);
        //        }
        //        ////-------------------------------------------------------------------------------------------------------
        //        //Log.Log.Info("开始加载 RabbitMQ 配置......");
        //        //RabbitMQBuliderExtensions.UseRabbitMQ("appsettings.json", "RabbitMQ");
        //        //Log.Log.Info("已经加载 RabbitMQ 配置......");

        //        //if (RabbitMQ.Consumers.ConsumerService.UseRabbitMQ)
        //        //{
        //        //    Log.Log.Info("开始订阅 RabbitMQ 消息......");
        //        //    new Common.WebApiCall().SubscribeMsg();
        //        //    Log.Log.Info("已经订阅 RabbitMQ 消息......");
        //        //}
        //    }
        //    catch (Exception ex)
        //    {
        //        Log.Log.Error(ex.ToString());
        //    }
        //    server.ChekckDynamicController = ChekckDynamicController;
        //    S_ServerHandler = server;
        //    return server;
        //}
        //#endregion

        //#region LoadApiController
        //public static void LoadApiController(ServerHandler server, ControllerInfo controllerInfo)
        //{
        //    if (controllerInfo == null || controllerInfo.ApiInfoList == null || controllerInfo.ApiInfoList.Count == 0) return;
        //    Log.Log.Warn($"正在注册{controllerInfo.ControllerName}Controller......");
        //    var methods = controllerInfo.ApiInfoList;

        //    SetHandler(methods, server);

        //    Log.Log.Warn($"已经注册{controllerInfo.ControllerName}Controller......");
        //}
        //#endregion

        //#region SetHandler
        ///// <summary>
        ///// 更新接口执行句柄
        ///// </summary>
        ///// <param name="methods"></param>
        ///// <param name="server"></param>
        //public static void SetHandler(List<ApiInfo> methods, ServerHandler server = null)
        //{
        //    if (server == null) server = Startup.serverHandler;
        //    if (methods == null || methods.Count == 0) return;
        //    //Log.Log.Warn($"正在注册{methodInfo.ControllerName}Controller......");
        //    foreach (var methodInfo in methods)
        //    {
        //        SetHandler(methodInfo, server);
        //    }
        //    //Log.Log.Warn($"已经注册{controllerBuilderInfo.ControllerName}Controller......");
        //}
        ///// <summary>
        ///// 更新接口执行句柄
        ///// </summary>
        ///// <param name="methodInfo"></param>
        ///// <param name="server"></param>
        //public static void SetHandler(ApiInfo methodInfo, ServerHandler server = null)
        //{
        //    if (server == null) server = Startup.serverHandler;

        //    if (methodInfo == null) return;
        //    //Log.Log.Warn($"正在注册{methodInfo.ControllerName}Controller......");
        //    //   foreach (var methodInfo in methods)
        //    {
        //        var methodName = methodInfo.ApiName;
        //        var baseRoute = methodInfo.BaseRoute;
        //        var httpMethods = methodInfo.HttpMethod;
        //        if (methodName.ToLower().Equals(httpMethods.ToLower()))
        //        {
        //            methodName = "";
        //        }
        //        var template = methodName;
        //        if (!string.IsNullOrEmpty(methodInfo.Route))
        //        {
        //            template = methodInfo.Route;
        //        }
        //        template = (baseRoute.TrimEnd('/') + "/" + template.Trim('/')).TrimEnd('/');
        //        //找出具有httphandler attribute的方法
        //        var handler = new HttpHandler
        //        {
        //            ControllerID = baseRoute,
        //            HandlerID = $"{methodInfo.ControllerName}_{methodInfo.ApiName}",
        //            Method = httpMethods,
        //            Path = template,
        //            Stat = 0,// methodInfo.Stat,
        //            Handler = (request, response) =>
        //            {
        //                object input = null;
        //                if (httpMethods.ToUpper().Trim() == "GET")
        //                    input = request.Query;
        //                else
        //                    input = request.DynamicBody();

        //                var rets = ApiExecutor.Executor(request, methodInfo.KeyWord, input);

        //                //response.HttpResponse.StatusCode = 200;
        //                return response.WriteJson(rets);
        //                //return rets;
        //            }
        //        };

        //        server.RemoveHandler(handler);
        //        server.AddHandler(handler);
        //    }
        //    //Log.Log.Warn($"已经注册{controllerBuilderInfo.ControllerName}Controller......");
        //}
        //#endregion

        //#region RemoveHandler
        ///// <summary>
        ///// 移除接口执行句柄
        ///// </summary>
        ///// <param name="methods"></param>
        ///// <param name="server"></param>
        //public static void RemoveHandler(List<ApiInfo> methods, ServerHandler server = null)
        //{
        //    if (server == null) server = Startup.serverHandler;
        //    if (methods == null || methods.Count == 0) return;
        //    foreach (var methodInfo in methods)
        //    {
        //        RemoveHandler(methodInfo, server);
        //    }
        //}
        ///// <summary>
        ///// 移除接口执行句柄
        ///// </summary>
        ///// <param name="methodInfo"></param>
        ///// <param name="server"></param>
        //public static void RemoveHandler(ApiInfo methodInfo, ServerHandler server = null)
        //{
        //    if (server == null) server = Startup.serverHandler;
        //    if (methodInfo == null) return;
        //    var template = methodInfo.ApiName;
        //    if (!string.IsNullOrEmpty(methodInfo.Route))
        //    {
        //        template = methodInfo.Route;
        //    }
        //    template = (methodInfo.BaseRoute + template.Trim('/')).TrimEnd('/');
        //    var handler = new HttpHandler
        //    {
        //        ControllerID = methodInfo.BaseRoute,
        //        HandlerID = $"{methodInfo.ControllerName}_{methodInfo.ApiName}",
        //        Method = methodInfo.HttpMethod,
        //        Path = template,
        //    };
        //    server?.RemoveHandler(handler);
        //}
        //#endregion
    }
}
